﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ChivalryUnityEditor
{
    class BatchRenameWindow : EditorWindow
    {
        [MenuItem("Tools/批量重命名")]
        public static void ShowWindow()
        {
            GetWindow(typeof(BatchRenameWindow), false, "批量重命名", true);
        }

        private Object[] m_allSelections;
        private string m_newName;
        private Stack<RenameHistory> m_historys;
        private Vector2 m_historyScroll;

        private void OnEnable()
        {
            Selection.selectionChanged += OnSelectionChanged;
            m_historys = new Stack<RenameHistory>();
        }

        private void OnDisable()
        {
            Selection.selectionChanged -= OnSelectionChanged;
            m_historys.Clear();
        }

        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            int selectCount = m_allSelections == null ? 0 : m_allSelections.Length;

            float labelWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 80;
            EditorGUILayout.PrefixLabel("已选择:" + selectCount + "个");
            m_newName = EditorGUILayout.TextField(m_newName);
            EditorGUIUtility.labelWidth = labelWidth;

            bool guiEnable = GUI.enabled;
            if (selectCount == 0)
                GUI.enabled = false;

            if (GUILayout.Button("重命名", GUILayout.MaxWidth(80)))
            {
                RenameHistory history = new RenameHistory();
                history.newName = m_newName;
                for (int i = 0; i < m_allSelections.Length; i++)
                {
                    RenameObject renameObject = new RenameObject();
                    renameObject.m_object = m_allSelections[i];
                    renameObject.m_oldPath = AssetDatabase.GetAssetPath(renameObject.m_object);
                    AssetDatabase.RenameAsset(renameObject.m_oldPath, m_newName + (i + 1));
                    history.m_renameObjects.Push(renameObject);
                }

                m_historys.Push(history);
            }
            GUI.enabled = guiEnable;
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Separator();

            //历史记录标题
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("历史记录:");

            guiEnable = GUI.enabled;
            if (m_historys.Count == 0)
                GUI.enabled = false;

            if (GUILayout.Button("撤销", GUILayout.MaxWidth(80)))
            {
                RenameHistory history = m_historys.Pop();
                foreach (var renameObject in history.m_renameObjects)
                {
                    string newPath = AssetDatabase.GetAssetPath(renameObject.m_object);
                    AssetDatabase.MoveAsset(newPath, renameObject.m_oldPath);
                }
            }
            GUI.enabled = guiEnable;
            EditorGUILayout.EndHorizontal();

            //历史记录
            m_historyScroll = EditorGUILayout.BeginScrollView(m_historyScroll);
            EditorGUILayout.BeginVertical(GUI.skin.box);
            if (m_historys.Count == 0)
            {
                EditorGUILayout.LabelField("暂无历史记录");
            }
            else
            {
                foreach (var history in m_historys)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(history.Count + "个文件:" + history.newName, GUILayout.ExpandWidth(true), GUILayout.MinWidth(100));
                    EditorGUILayout.LabelField(System.DateTime.Now.ToShortTimeString(), GUILayout.MaxWidth(50));
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.Separator();
                }
            }
            EditorGUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
        }

        private void OnSelectionChanged()
        {
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
            System.Array.Sort<Object>(selection, (a, b) =>
            {
                return a.name.CompareTo(b.name);
            });
            m_allSelections = selection;
            Repaint();
        }

        private class RenameHistory
        {
            public Stack<RenameObject> m_renameObjects = new Stack<RenameObject>();
            public int Count
            {
                get { return m_renameObjects.Count; }
            }
            public string newName;
        }

        private class RenameObject
        {
            public Object m_object;
            public string m_oldPath;
        }
    }
}